package com.zte.purchase.control;


import com.zte.market.pojo.City;
import com.zte.market.pojo.Custom;
import com.zte.market.pojo.Province;
import com.zte.market.service.CityService;
import com.zte.market.service.ProvinceService;
import com.zte.purchase.pojo.PurchaseCFirm;
import com.zte.purchase.service.impl.PurchaseImpl;
import com.zte.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/manufacturer")
@SessionAttributes({"provinceList","firmPage"})
public class ManufacturerAction {
    @Autowired
    private CityService cityService;

    @Autowired
    private PurchaseImpl purchase;

    @Autowired
    private ProvinceService provinceService;

    /**
     * Ajax请求返回 城市数据
     * @param provinceId
     * @return
     */
    @RequestMapping("/queryCityListByAjax")
    @ResponseBody
    public List<City> queryCityListByAjax(Integer provinceId){
        List<City> cities = cityService.queryCitys(provinceId);
        return cities;
    }

    /**
     * 查询厂商信息
     * @param purchaseCFirm
     * @return
     */
    @RequestMapping("/queryByParam")
    private String queryByParam(PurchaseCFirm purchaseCFirm, Integer provinceId, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "7")Integer pageSize, Model model, HttpSession session){
        List<City> cityList = new ArrayList<>();//用于作为参数进行查询

        List<City> cityByProvinceId = new ArrayList<>();//用于回现城市数据
        if(purchaseCFirm == null || "".equals(purchaseCFirm)){
            purchaseCFirm = null;
        }else{
            if(purchaseCFirm.getFirmCity() == null || "".equals(purchaseCFirm.getFirmCity())){
                if( provinceId != null && !"".equals(provinceId)){
                    cityList = cityService.queryCitys(provinceId);
                }
            }else {
                City city = cityService.queryById(purchaseCFirm.getFirmCity());
                if(city != null){
                    cityList.add(city);
                }
            }
        }
        if( provinceId != null && !"".equals(provinceId)){
            cityByProvinceId = cityService.queryCitys(provinceId);
        }
        //全部省份列表
        List<Province> provinceList = provinceService.queryProvinces();
        //给定页面尺寸
        PageModel<PurchaseCFirm> firmPage =  purchase.queryByParam(purchaseCFirm,cityList,pageSize,pageNum);
        //回显
        model.addAttribute("provinceList",provinceList);
        model.addAttribute("provinceId",provinceId);
        model.addAttribute("firmPage",firmPage);
        model.addAttribute("purchaseCFirm",purchaseCFirm);
        model.addAttribute("provinceId",provinceId);
        model.addAttribute("cityByProvinceId",cityByProvinceId);
        return "purchase/manufacturer/manufacturerList";
    }
}
